package com.zeng.ssm.service.support.trans.scanner;


import com.zeng.ssm.service.support.trans.base.IClientHandler;
import com.zeng.ssm.service.support.trans.base.IServerHandler;
import com.zeng.ssm.service.util.ApplicationContextUtil;
import org.springframework.util.Assert;

/**
 * Handler工厂
 * <p>
 * 作用：从spring容器中获取Handler
 *
 * @author zengwanlin
 * @date 2022-06-24T15:48
 */
public class TransHandlerFactory {

    /**
     * 别名前缀
     */
    public static final String ALIAS_PREFIX = "TRANS_HANDLER_";

    /**
     * 获取客户端Handler
     *
     * @param sysCode 系统编码
     * @param msgCode 消息编码
     * @return
     * @throws org.springframework.beans.BeansException
     */
    public static IClientHandler getClientHandler(String sysCode, String msgCode) {
        return ApplicationContextUtil.getBean(getAlias(sysCode, msgCode), IClientHandler.class);
    }

    /**
     * 获取服务端Handler
     *
     * @param sysCode 系统编码
     * @param msgCode 消息编码
     * @return
     * @throws org.springframework.beans.BeansException
     */
    public static IServerHandler getServerHandler(String sysCode, String msgCode) {
        return ApplicationContextUtil.getBean(getAlias(sysCode, msgCode), IServerHandler.class);
    }

    /**
     * 获取别名
     *
     * @param sysCode 系统编码
     * @param msgCode 消息编码
     * @return SpringBean别名
     */
    public static String getAlias(String sysCode, String msgCode) {
        Assert.hasText(sysCode, "系统编码不能为空");
        Assert.hasText(msgCode, "消息编码不能为空");
        return ALIAS_PREFIX + sysCode + "_" + msgCode;
    }

}
